/**
 * 日代管理系统
 * 
 * @date 2018年01月14日
 * @author duzh
 *
 * 版权所有：经纶传媒股份有限公司
 */
package cn.dg.manager.login.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;

import cn.dg.manager.base.BaseController;
import cn.dg.manager.base.consts.Const;
import cn.dg.manager.base.util.AppUtil;
import cn.dg.manager.base.util.PageData;
import cn.dg.manager.login.bean.MenuTreeNodeBean;
import cn.dg.manager.login.entity.User;
import cn.dg.manager.login.service.UserService;


/**
 * 全局首页Action
 * 
 * @author duzh
 *
 */
@Controller
public class LoginAction  extends BaseController {

	/**
	 * 序列化
	 */
	private static final long serialVersionUID = 4994460933999185028L;

	@Autowired
	private UserService userService;

	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping(value="/index")
	public String index() {
		// TODO session 记录 保持
//		if (!ShiroKit.authenticated()) {
//			return "login";
//		}
		return "main";
	}

	/**
	 * 欢迎页
	 * 
	 * @return
	 */
	@RequestMapping(value="/protal")
	public String portal() {
		return "portal";
	}

	/**
	 * 修改密码页
	 * 
	 * @return
	 */
	@RequestMapping(value="/changePwd")
	public String pwdShow() {
		return "changePwd";
	}

	/**
	 * 修改密码
	 */
	@RequestMapping(value="/pwdChange")
	public Object pwdChange() {
		Integer result = 0;
		PageData pd = new PageData();
		pd = this.getPageData();
		
		String oldPassword = pd.getString("oldPassword");
		String newPassword = pd.getString("newPassword");
		
		// TODO String userId = SessionUtil.getCurrentUserId();
		String userId = "1";
		
		if(!StringUtils.isEmpty(userId) && !StringUtils.isEmpty(oldPassword) && !StringUtils.isEmpty(newPassword)) {
			// result = userService.updatePassword(oldPassword, newPassword, Long.parseLong(userId));
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		return AppUtil.returnObject(pd, map);
	}

	/**
	 * 登录页
	 * 
	 * @return
	 */
	@RequestMapping(value="/login")
	public ModelAndView login() {
		String errInfo = Const.RESULT_JSON_SUCCESS;
		// TODO 已经登录了，则直接跳转
//		if (ShiroKit.authenticated()) {
//			return "index";
//		}
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		mv.addObject("pd", pd);
		
		if (StringUtils.isEmpty(pd.getString("username"))) {
			mv.setViewName("login");
			return mv;
		}
		// 未登录时，看看是否有错误信息
		User user = userService.findUserByUserId(pd.getString("username"));
		if(!StringUtils.equals(pd.getString("password"),user.getPassword())) {
			errInfo = Const.RESULT_JSON_ERROR;
			mv.addObject("message", "登陆密码错误，请重新输入！");
			mv.setViewName("login");
		} else {
			mv.setViewName("main");
		}
		// 返回结果
		mv.addObject("result", errInfo);
		return mv;
	}

	/**
	 * 退出
	 * 
	 * @return
	 */
	@RequestMapping(value="/logout")
	@ResponseBody
	public String logout() {
		//TODO ShiroKit.stop(true);
		return "login";
	}

	/**
	 * 获取系统菜单
	 */
	@RequestMapping(value="getMenuTree")
	@ResponseBody
	public Object getMenuTree() {
		Map<String, Object> map = new HashMap<String, Object>();
		
		// 获取根目录
		List<MenuTreeNodeBean> menus = new ArrayList<MenuTreeNodeBean>();
		// TODO 菜单数据库
		// 欢迎页
		MenuTreeNodeBean salesMenu = new MenuTreeNodeBean();
		salesMenu.setMenuName("商品销售情况管理");
		List<MenuTreeNodeBean> salesChildMenu =  new ArrayList<MenuTreeNodeBean>();
		MenuTreeNodeBean goodsSales = new MenuTreeNodeBean();
		goodsSales.setMenuName("商品销售情况查看");
		goodsSales.setMenuAction("/salesIndex");
		goodsSales.setMenuCode("goods_sales");
		salesChildMenu.add(goodsSales);
		salesMenu.setChildrens(salesChildMenu);
		menus.add(salesMenu);
		
		// 菜单情报管理
		MenuTreeNodeBean goodsMenu = new MenuTreeNodeBean();
		goodsMenu.setMenuName("商品基本情报管理");
		List<MenuTreeNodeBean> childMenu =  new ArrayList<MenuTreeNodeBean>();
		MenuTreeNodeBean goodsclassify = new MenuTreeNodeBean();
		goodsclassify.setMenuName("商品分类基本信息");
		goodsclassify.setMenuAction("/goodsClassify/index");
		goodsclassify.setMenuCode("goods_classify");
		childMenu.add(goodsclassify);
		
		MenuTreeNodeBean goodsBase = new MenuTreeNodeBean();
		goodsBase.setMenuName("商品基本信息");
		goodsBase.setMenuAction("/goodsBaseIndex");
		goodsBase.setMenuCode("goods_base");
		childMenu.add(goodsBase);
		goodsMenu.setChildrens(childMenu);
		menus.add(goodsMenu);
		
		// 商品入库管理
		MenuTreeNodeBean warehouseMenu = new MenuTreeNodeBean();
		warehouseMenu.setMenuName("商品入库情报管理");
		List<MenuTreeNodeBean> warehouseChildMenu =  new ArrayList<MenuTreeNodeBean>();
		MenuTreeNodeBean goodsWarehouse = new MenuTreeNodeBean();
		goodsWarehouse.setMenuName("商品入库信息");
		goodsWarehouse.setMenuAction("/warehouse/index");
		goodsWarehouse.setMenuCode("goods_warehouse");
		warehouseChildMenu.add(goodsWarehouse);
		warehouseMenu.setChildrens(warehouseChildMenu);
		menus.add(warehouseMenu);
		
		// 订单管理
		MenuTreeNodeBean orderMenu = new MenuTreeNodeBean();
		orderMenu.setMenuName("商品订单管理");
		List<MenuTreeNodeBean> orderChildMenu =  new ArrayList<MenuTreeNodeBean>();
		MenuTreeNodeBean goodsOrder = new MenuTreeNodeBean();
		goodsOrder.setMenuName("商品订单信息");
		goodsOrder.setMenuAction("/order/index");
		goodsOrder.setMenuCode("goods_order");
		orderChildMenu.add(goodsOrder);
		orderMenu.setChildrens(orderChildMenu);
		menus.add(orderMenu);
		
		// 订单管理
		MenuTreeNodeBean deliveryMenu = new MenuTreeNodeBean();
		deliveryMenu.setMenuName("发货信息管理");
		List<MenuTreeNodeBean> deliveryChildMenu =  new ArrayList<MenuTreeNodeBean>();
		MenuTreeNodeBean deliveryOrder = new MenuTreeNodeBean();
		deliveryOrder.setMenuName("订单发货信息一览");
		deliveryOrder.setMenuAction("/deliveryIndex");
		deliveryOrder.setMenuCode("goods_delivery");
		deliveryChildMenu.add(deliveryOrder);
		deliveryMenu.setChildrens(deliveryChildMenu);
		menus.add(deliveryMenu);
		
		// 地址管理
		MenuTreeNodeBean addressMenu = new MenuTreeNodeBean();
		addressMenu.setMenuName("用户地址信息管理");
		List<MenuTreeNodeBean> addressChildMenu =  new ArrayList<MenuTreeNodeBean>();
		MenuTreeNodeBean address = new MenuTreeNodeBean();
		address.setMenuName("用户地址信息一览");
		address.setMenuAction("/user/address/index");
		address.setMenuCode("user_address");
		addressChildMenu.add(address);
		addressMenu.setChildrens(addressChildMenu);
		menus.add(addressMenu);
				
		map.put("menus", menus);
		return AppUtil.returnObject(new PageData(), map);
	}

}
